<?php
namespace App\Pull;

use App\Http\Controllers\Wechat\LoginController;
use App\Librarys\Tencent;

class Center extends Base
{

    private $configUrl;
    private $appId;
    private $secret;
    private $code2session_url;
    const code = 10000; //sessionKey 失败码

    public function __construct()
    {
        if (empty($config)){
            $config = config('wechat');
        }
        $this->appId = $config['appid'];
        $this->secret = $config['secret'];
        $this->code2session_url = $config['code2Session_url'];
        $this->configUrl = config('urls.ucenter_api');
    }

    /**
     * 获取session_key
     * Notes:
     * User: aniu
     * Date: 2020/6/9
     * Time: 14:47
     */
    public function authCodeAndCode2session($code)
    {
        $code2session_url = sprintf($this->code2session_url, $this->appId, $this->secret, $code);
        $userInfo = $this->call('get', $code2session_url);
        $userInfo = $userInfo [1];
        if (!isset($userInfo['session_key'])){
            return[
                'code' => self::code,
                'msg' => '获取 session_key 失败',
            ];
        }
        return $userInfo;
    }

    /**
     * Notes:
     * User: aniu
     * Date: 2020/6/24
     * Time: 11:43
     * @param $json
     * @return mixed
     */
    public function requestAPI($json)
    {
        $token = new LoginController();
        $user = new Tencent();
        $access_token = $token->get_accessToken();
        /*
         * POST发送https请求客服接口api
         */
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
        $res = $user->wechatCurl($url, $json);
        return $res[1];
    }
}
